//
//  DataPacket.m
//  Proto
//
//  Created by kingly on 15/11/23.
//  Copyright © 2015年 Will. All rights reserved.
//

/**
 *  
 ＊ 协议头(proto内容长度(四字节)+proto内容(二进制)
 ＊
 **/

#import "DataPacket.h"

@interface DataPacket (){
    

    NSUInteger protoDataLength;      //protoData的长度
    NSData *gzipData;    //压缩Data
  
}
@end

@implementation DataPacket


#pragma mark - requestDataToByteStream
/**
 * 发送包的二进制流
 */
- (NSData*) requestDataToByteStream{
    NSMutableData* dataCache = [[NSMutableData alloc] init];//数据缓存
    protoDataLength = _protoDataContent.length;
    Byte byte[] = {[self int0:protoDataLength],
                   [self int1:protoDataLength],
                   [self int2:protoDataLength],
                   [self int3:protoDataLength]};
    [dataCache appendBytes:byte length:4];
    [dataCache appendData:_protoDataContent];
    
    return [NSData dataWithData:dataCache];
}

/**
 * take the package
 */


#pragma mark － Byte
- (Byte) int3:(NSUInteger)x
{
    return (Byte) (x >> 24);
}

- (Byte) int2:(NSUInteger)x
{
    return (Byte) (x >> 16);
}

- (Byte) int1:(NSUInteger)x
{
    return (Byte) (x >> 8);
}

- (Byte) int0:(NSUInteger)x
{
    return (Byte) (x);
}

@end
